﻿using LbangCMS.EFModels;
using LbangCMS.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LbangCMS.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            var model = new IndexModel();
            var companyInfo = GetCompanyInfo();
            var news = GetLastNews();
            var products = GetRecommendProduct();
            var about = GetSinglePage("about");
            model.About = ReplaceHtmlTag(about.Body, 150);
            model.Address = companyInfo.Address;
            model.Phone = companyInfo.Phone;
            model.Address = companyInfo.Address;
            model.Zipcode = companyInfo.Zipcode;
            model.News = new List<IndexNewsModel>();
            var index = 0;
            foreach (var item in news)
            {
                model.News.Add(new IndexNewsModel() { ID=item.ID,ImageSrc=item.CoverSrc,IsMain=(index==0),PublishTime=item.PublishTime,Summary= ReplaceHtmlTag(item.Summary,50),Title=item.Title });
                index++;
            }
            model.Products = new List<IndexProductModel>();
            foreach (var item in products)
            {
                model.Products.Add(new IndexProductModel() { ID = item.ID, ImageSrc = item.ImageSrc, ProductName = item.Name });
            }
            return View(model);
        }

        /// <summary>
        /// 单页
        /// </summary>
        /// <returns></returns>
        public ActionResult SinglePage(string code)
        {
            var page = GetSinglePage(code);
           
            if(page==null)
            {
                return RedirectToAction("NotFound");
            }
            else
            {
                SinglePageModel model = new SinglePageModel();
                model.Code = code.ToLower();
                model.Title = page.Title;
                model.Body = page.Body;
                string[] navCodes = { "about", "culture", "brand", "develop", "honor", "event", "base", "society", "contact" };
                model.Navs = new List<SinglePageNav>();
                foreach (var item in navCodes.ToList())
                {
                    var p = GetSinglePage(item);
                    if (p != null)
                    {
                        model.Navs.Add(new SinglePageNav() { Code=p.Code,Title=p.Title });
                    }
                }
                ViewBag.Title = page.Title;
                return View(model);
            }
            
        }

        [ChildActionOnly]
        public ActionResult ContactInfo()
        {
            var companyInfo = GetCompanyInfo();
            if (companyInfo != null)
            {
                return PartialView("_ContactInfo", new ContactInfoModel { Address=companyInfo.Address,CompanyName=companyInfo.CompanyName,Fax=companyInfo.Fax,Phone=companyInfo.Phone,Zipcode=companyInfo.Zipcode });
            }
            else
                return Content("");
        }

        public ActionResult NotFound()
        {
            return Content("页面不存在");
        }
        

        [ChildActionOnly]
        public ActionResult CopyRight()
        {
            using (var db = new Database())
            {
                var siteInfo = db.SiteInfo.SingleOrDefault();
                var companyInfo = db.CompanyInfo.SingleOrDefault();
                var model = new LbangCMS.ViewModels.CopyRightModel()
                {
                    Address=companyInfo.Address,
                    Phone=companyInfo.Phone,
                    BeianNo = siteInfo.BeianNo,
                    CompanyName=companyInfo.CompanyName
                };
                return PartialView("_CopyRight", model);
            }
        }

        public ActionResult ContactPhone()
        {
            using (var db = new Database())
            {
                var companyInfo = db.CompanyInfo.SingleOrDefault();
                return PartialView("_ContactPhone", companyInfo.Phone);
            }
        }

        #region private

        private string ReplaceHtmlTag(string html, int length = 0)
        {
            string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");
            strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");

            if (length > 0 && strText.Length > length)
                return strText.Substring(0, length);

            return strText;
        }
        private CompanyInfo GetCompanyInfo()
        {
            using (var db = new Database())
            {
                return db.CompanyInfo.FirstOrDefault();
            }
        }
        private SinglePage GetSinglePage(string code)
        {
            using (var db = new Database())
            {
                return db.SinglePages.Where(o => o.Code == code).FirstOrDefault();
            }
        }

        private List<Product> GetRecommendProduct()
        {
            using (var db = new Database())
            {
                return db.Products.Where(o => o.IsRecommend).ToList();
            }
        }

        private List<Content> GetLastNews()
        {
            using (var db = new Database())
            {
                return db.Contents.Where(o => o.Status == 1).OrderByDescending(o => o.ID).Take(4).ToList();
            }
        }

        public List<Banner> GetIndexBanner()
        {
            using (var db = new Database())
            {
                return db.Banners.ToList();
            }
        }


        #endregion


    }
}